package com.example.demo.common.response;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * Date Utility Class used to convert Strings to Dates and Timestamps
 *
 */
public final class DateUtil {

	private final static String shortDateFmt = "yyyy-MM-dd";
	private final static String dateFmt = "yyyy-MM-dd HH:mm:ss";
	private final static String longDateFmt = "yyyy-MM-dd HH:mm:ss.SSS";

	public static void main(String[] args) throws ParseException {

		System.out.println(DateUtil.getComputeDayStr("2017-02-12",6,"yyyy-MM-dd"));
	}

	public static String longfmtNow() {
		SimpleDateFormat sdf = new SimpleDateFormat(longDateFmt);
		return sdf.format(new Date());
	}

	public static Date longfmtToDate(String dstr) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(longDateFmt);
		return sdf.parse(dstr);
	}

	public static Date dateStringNextDayDate(String date) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(shortDateFmt);
		Date dt2 = sdf.parse(date);
		long lTime = dt2.getTime() + 86400000;
		return new Date(lTime);
	}

	/**
	 * @return
	 * @throws ParseException
	 */
	public static long dateStringToSecond(String date) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(dateFmt);
		Date dt2 = sdf.parse(date);
		long lTime = dt2.getTime() / 1000;
		return lTime;
	}

	public static String dateStringToDateString(Date date)
			throws ParseException {
		long time = date.getTime();
		return timeStampToDateString(time);
	}

	public static Date dateStringToDate(String date) throws ParseException {
		SimpleDateFormat sdf = null;
		if (date.length() > 10) {
			sdf = new SimpleDateFormat(dateFmt);
		} else {
			sdf = new SimpleDateFormat(shortDateFmt);
		}
		Date dt2 = sdf.parse(date);
		System.err.println(dt2.getTime());
		return dt2;
	}

	public static String timeStampToDateString(long timeStamp) {
		SimpleDateFormat df = new SimpleDateFormat(dateFmt);
		Timestamp now = new Timestamp(timeStamp);
		String str = df.format(now);
		return str;
	}

	public static String timeStamp() {
		return ((Long) System.currentTimeMillis()).toString();
	}

	public static String caculateTime(Date actStartTime, Date actEndTime) {
		if (actStartTime == null) {
			return null;
		}
		long end = 0;
		if (actEndTime == null) {
			end = System.currentTimeMillis();
		} else {
			end = actEndTime.getTime();
		}
		long start = actStartTime.getTime();
		actEndTime.getTime();
		long minutes = (end - start) / 60000;
		System.err.println(minutes);
		long day = 0;
		long hour = 0;
		long min = 0;
		day = minutes / (60 * 24);
		hour = (minutes % (60 * 24)) / 60;
		min = (minutes % (60 * 24)) % 60;
		String str = "";
		if (day != 0) {
			str += day + "天" + hour + "小时" + min + "分钟";
		} else {
			if (hour != 0) {
				str += hour + "小时" + min + "分钟";
			} else {
				str += min + "分钟";
			}
		}
		return str;
	}

	/**
	 * 指定时间推移前(后)几天
	 * 
	 * @param date
	 *            时间
	 * @param amount
	 *            几天
	 * @return
	 */
	public static Date computeWithDay(Date date, int amount) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_YEAR, amount);
		return cal.getTime();
	}

	public static String toString(Date date, String pattern) {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(date);
	}

	public static Date valueOf(String str, String pattern)
			throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.parse(str);
	}

	/**
	 * 指定时间字符串推移前(后)几天
	 * 
	 * @param date
	 *            时间
	 * @param amount
	 *            几天
	 * @param pattern
	 *            转换格式
	 * @return
	 * @throws ParseException
	 */
	public static String getComputeDayStr(String date, int amount,
			String pattern) throws ParseException {
		return DateUtil.toString(DateUtil.computeWithDay(
				DateUtil.valueOf(date, pattern), amount), pattern);
	}
	

	public static Date getFormatDate(String date, String format) throws Exception{
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		Date fdate = formatter.parse(date);
		return fdate;
	}
}
